<?php
namespace App\Models\Chain\Invite;


use App\Models\Factory\InviteFactory;
use App\Models\Chain\AbstractHandler;
use App\Models\Factory\UserCreditFactory;
use App\Models\Factory\UserFactory;
use App\Models\Orm\UserCreditTypeUser;
use App\Models\Orm\UserInvite;


/**
 * 检查邀请码是否正确
 * @package App\Models\Chain\Invite
 */

class CheckUserInviteAction extends AbstractHandler
{
    private $params = array();
    protected $error = array('error' => '邀请码错误', 'code' => 7011);


    public function __construct($params)
    {
        $this->params = $params;
    }


    public function handleRequest()
    {
        if ($this->CheckUserInvite($this->params) == true) {
            $this->setSuccessor(new UpdateUserInviteAction($this->params));
            return $this->getSuccessor()->handleRequest();
        } else {
            return $this->error;
        }
    }

    public function CheckUserInvite($params)
    {
        $invite = $params['inviteCode'] - 1902;
        //是否为有效的邀请码
        $result = UserFactory::getUserId($invite);

        $params['user_id'] = $invite;

        $this -> params = $params;

        return $result;
    }

}